<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>
            <el-button v-if="isAuth('kernel:role:form')" type="success" icon="Plus" @click="router.push({ name: 'kernel_role_form' })">
              {{ i18n('common.create') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_main_block_select">
        <div class="input">
          <el-select
            v-model="dataForm.organization_id"
            :placeholder="i18n('common.please_select') + i18n('organization.title')"
            clearable>
            <el-option :label="i18n('common.all')" value=""></el-option>
            <el-option v-for="(v, k) in organizationList" :label="v.title" :key="k" :value="v.id">
            </el-option>
          </el-select>
        </div>
        <div class="input">
          <el-input v-model="dataForm.title" :placeholder="i18n('common.please_input') + i18n('user.role.title')" clearable>
          </el-input>
        </div>
        <div class="date">
          <el-date-picker v-model="dataForm.create_time" type="daterange" :range-separator="i18n('common.to')" :start-placeholder="i18n('common.start_time')" :end-placeholder="i18n('common.end_time')" clearable></el-date-picker>
        </div>
        <div class="input">
          <el-button icon="Search" @click="getDataList(true)">
            {{ i18n('common.search') }}
          </el-button>
        </div>
      </div>

      <div class="admin_table_main">
        <el-table :data="dataList" v-loading="dataListLoading" @selection-change="selectionChangeHandle" stripe>
          <el-table-column type="selection" header-align="center" align="center">
          </el-table-column>

          <el-table-column prop="id" :label="i18n('common.id')" fixed="left" width="70">
          </el-table-column>

          <el-table-column :label="i18n('organization.title')" width="140">
            <template v-slot="scope">
              <span v-if="scope.row.organization">
                {{ scope.row.organization.title }}
              </span>
              <span v-else>
                平台角色
              </span>
            </template>
          </el-table-column>

          <el-table-column prop="title" :label="i18n('user.role.title')">
          </el-table-column>

          <el-table-column prop="content" :label="i18n('user.role.content')">
          </el-table-column>

          <el-table-column :label="i18n('common.handle')" fixed="right" width="200">
            <template v-slot="scope">
              <el-button v-if="isAuth('kernel:role:form')" type="primary" icon="Edit" @click="router.push({ name: 'kernel_role_form', query: { id: scope.row.id } })">
                {{ i18n('common.update') }}
              </el-button>

              <el-button v-if="isAuth('kernel:role:delete') && 1 != scope.row.id" type="danger" icon="Delete" @click="deleteHandle(scope.row.id)">
                {{ i18n('common.delete') }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <div class="admin_table_main_pagination">
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :page-size="pageSize"
            :total="totalPage"
            :current-page="pageIndex"
            background
            layout="prev, pager, next,jumper,total">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup name="role-list">
  import { useBase } from "@/hooks/base/useBase"
  import { useTable } from "@/hooks/table/useTable"

  const { router, i18n, isAuth } = useBase()

  const model = ref('role')

  const organizationList = ref([])

  const dataForm = reactive([
    'organization_id',
    'title',
    'create_time',
  ])

  const {
    dataList,
    pageIndex,
    pageSize,
    totalPage,
    dataListLoading,
    sizeChangeHandle,
    currentChangeHandle,
    selectionChangeHandle,
    getDataList,
    deleteHandle,
  } = useTable(model.value, dataForm)

  const loadOrganizationList = () => {
    $http({
      url: $http.adornUrl('/organization/select'),
      method: 'get',
    }).then(({ data }) => {
      if (data && data.status === 200) {
        organizationList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  onMounted(() => {
    getDataList(model)

    loadOrganizationList();
  })
</script>
